<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="课程代码">
              <a-input v-model="queryParam.kch"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="课程名称">
              <a-input v-model="queryParam.kcmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="培养单位">
                <a-select :disabled="selectMenu.yx.length === 1" allowClear v-model="queryParam.kcssyxm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="batchCheck">批量审核</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <p style="color:#EC0000">温馨提示：此处查询的结果是所有未提交和审核不通过的课程信息</p>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x:1200}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="operation" slot-scope="a,b">
          <a @click="showDetail(b)">查看明细</a>
          <a-divider type="vertical" />
          <a @click="toCheck(b)">审核</a>
        </template>
      </s-table>
    </a-card>

    <course-detail :visible="detailVisible" @cancel="detailVisible = false" :confirmLoading="confirmLoading" :courseInfo="courseInfo"/>

    <course-batch-check :visible="checkVisible" :ids="ids" @cancel="checkVisible = false" @doRefresh="doRefresh" :confirmLoading="checkLoading" />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import courseDetail from '../courseDetail'
import courseBatchCheck from '../courseBatchCheck'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      ids: [],
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '课程代码',
          dataIndex: 'kch'
        },
        {
          title: '课程名',
          dataIndex: 'kcmc'
        },
        {
          title: '英文名',
          dataIndex: 'kcywmc',
          width:260
        },
        {
          title: '培养单位',
          dataIndex: 'ssyxmc'
        },
        {
          title: '课程层次',
          dataIndex: 'kcccmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width:160,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.courseCheckList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yx:[]
      },
      detailVisible: false,
      checkVisible: false,
      checkLoading: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable, courseDetail, courseBatchCheck},
  computed: {
    ...mapState({
    })
  },
  created () {
    this.initSelectMenu()
  },
  methods: {
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'kcssyxm', this.selectMenu.yx[0].dwh)
        }
        this.initDone = true
        // this.$nextTick(() => {
        //   this.$refs.table.refresh(true)
        // })
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    showDetail (course) {
      this.confirmLoading = true
      this.$api.cultivate.courseDetail(course.id).then(res => {
        this.confirmLoading = false
        this.courseInfo = res.data
      })
      this.detailVisible = true
    },
    toCheck (c) {
      this.$router.push(`/cultivate/course/info/${c.id}/check`)
    },
    batchCheck () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要审核的课程')
        return
      }
      this.ids = this.selectedRows.map(i => {
        return i.id
      })
      this.checkVisible = true
    },

    doRefresh(){
      this.selectedRowKeys = []
      this.selectedRows = []
      this.$refs.table.refresh(false)
    }
  }
}
</script>

<style>

</style>
